package com.lyk.read.book.ui.main

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lyk.common.base.BaseDialog
import com.lyk.common.dialog.NormalDialog
import com.lyk.common.util.AppDataUtil
import com.lyk.common.util.GsonUtil.jsonToList
import com.lyk.common.util.GsonUtil.toJson
import com.lyk.read.book.R
import com.lyk.read.book.base.BaseBookActivity
import com.lyk.read.book.bean.ItemBean
import com.lyk.read.book.dialog.AddOrEditDialog
import com.lyk.read.book.ui.detail.DetailActivity
import java.io.File

/**
 * 打包信息：文件：根目录/readBook，alias：readBook，password：123456
 * 压缩：将图片目录全部打包，包括承载的包文件，再将json数据写入放入包文件同级。一起打包压缩
 * 解压：解压到指定文件即可
 */
class MainActivity : BaseBookActivity() {

    companion object {
        const val CACHE_LIST_KEY = "list_json"
    }

    private val recyclerView: RecyclerView by lazy { findViewById(R.id.main_recycler) }
    private val addBtn: Button by lazy { findViewById(R.id.add_btn) }

    private val adapter: MainAdapter by lazy { MainAdapter(list) }

    private val list: MutableList<ItemBean> = mutableListOf()

    override fun getLayoutId(): Int {
        return R.layout.layout
    }

    override fun initView() {
        setPlaceView(recyclerView)
        showPlaceView(1)

        recyclerView.layoutManager = LinearLayoutManager(mContext)
        recyclerView.adapter = adapter

        initEvent()
    }

    private fun initEvent() {
        adapter.setOnItemLongClickListener { _, _, position ->
            val dialog = NormalDialog().apply {
                arguments = Bundle().apply {
                    putString(NormalDialog.KEY_TITLE, "提示")
                    putString(NormalDialog.KEY_CANCEL, "删除")
                    putString(NormalDialog.KEY_CONFIRM, "修改")
                    putString(NormalDialog.KEY_CONTENT, "您是要修改还是删除该条记录？")
                }
                onCancelCallBack = object : BaseDialog.CancelCallBack {
                    override fun onCancel(value: String) {
                        list.removeAt(position).run {
                            img?.let { File(it).delete() }
                            AppDataUtil.deleteForKey(title ?: "")
                        }
                        setListData(list)
                        AppDataUtil.putString(CACHE_LIST_KEY, list.toJson())
                    }
                }
                onConfirmCallBack = object : BaseDialog.ConfirmCallBack {
                    override fun onConfirm(value: String) {
                        AddOrEditDialog().apply {
                            arguments = Bundle().apply {
                                putInt("type", 1)
                                putParcelable("mainItem", list[position])
                            }
                            myConfirmCallBack = { bean ->
                                list[position] = bean
                                setListData(list)
                                AppDataUtil.putString(CACHE_LIST_KEY, list.toJson())
                            }
                            show(supportFragmentManager, "")
                        }
                    }
                }
            }
            dialog.show(supportFragmentManager, "")
            true
        }
        adapter.setOnItemClickListener { _, _, position ->
            startActivity(Intent(this, DetailActivity::class.java).apply {
                putExtra("list_item", list[position])
            })
        }

        addBtn.setOnClickListener {
            AddOrEditDialog().apply {
                arguments = Bundle().apply {
                    putInt("type", 0)
                }
                myConfirmCallBack = { bean ->
                    list.add(bean)
                    setListData(list)
                    AppDataUtil.putString(CACHE_LIST_KEY, list.toJson())
                }
                show(supportFragmentManager, "")
            }
        }
    }

    override fun initData() {
        val json = AppDataUtil.getString(CACHE_LIST_KEY)
        setListData(json.jsonToList(ItemBean::class.java), true)
    }

    private fun setListData(data: List<ItemBean>?, isAddAll: Boolean = false) {
        data?.takeIf { it.isNotEmpty() }?.let {
            if (isAddAll) {
                list.clear()
                list.addAll(it)
            }
            adapter.notifyDataSetChanged()
            hidePlaceView()
        } ?: showPlaceView(2)
    }

}